package com.dove.ohpm.service

import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.Messages
import org.jetbrains.plugins.terminal.ShellTerminalWidget
import org.jetbrains.plugins.terminal.TerminalToolWindowManager

class Command {
    companion object {
        private var terminalWidget: ShellTerminalWidget? = null
        private var terminalView: TerminalToolWindowManager? = null
        fun execute(command: String, path: String) {
            try {
                terminalView = ProjectLoader.project?.let { TerminalToolWindowManager.getInstance(it) }
                if (terminalView != null && terminalWidget == null)
                    terminalWidget = terminalView!!.createLocalShellWidget(ProjectLoader.project?.basePath, "Ohpm")
                if (terminalWidget != null) {
                    if (System.getProperty("os.name").lowercase().contains("windows"))
                        terminalWidget!!.executeCommand("cd ${path.substring(0, 1)}:\\")
                    terminalWidget!!.executeCommand("cd $path")
                    terminalWidget!!.executeCommand(command)
                }
            } catch (ex: Exception) {
                Messages.showErrorDialog(null as Project?, ex.message, "Error")
            }
        }
    }
}